package io.renren.modules.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.demo.dao.DhGodsInfoDao;
import io.renren.modules.demo.dao.DhGodsrentInfoDao;
import io.renren.modules.demo.dto.DhGodsrentInfoDTO;
import io.renren.modules.demo.entity.DhGodsInfoEntity;
import io.renren.modules.demo.entity.DhGodsrentInfoEntity;
import io.renren.modules.demo.service.DhGodsrentInfoService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@Service
public class DhGodsrentInfoServiceImpl extends CrudServiceImpl<DhGodsrentInfoDao, DhGodsrentInfoEntity, DhGodsrentInfoDTO> implements DhGodsrentInfoService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private DhGodsrentInfoDao dhGodsrentInfoDao;

    @Autowired
    private DhGodsInfoDao dhGodsInfoDao;

    @Override
    public QueryWrapper<DhGodsrentInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<DhGodsrentInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 获取当前登录账号的租用物品信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<DhGodsrentInfoDTO> getRentInfoDataList(Long userId) {
        SysUserDTO sysUserDTO = sysUserService.getCLOUusername(userId);
        Integer username = Integer.valueOf(sysUserDTO.getUsername());
        LambdaQueryWrapper<DhGodsrentInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhGodsrentInfoEntity::getUserId, username);
        List<DhGodsrentInfoEntity> dhGodsrentInfoEntities = dhGodsrentInfoDao.selectList(queryWrapper);
        return ConvertUtils.sourceToTarget(dhGodsrentInfoEntities, DhGodsrentInfoDTO.class);
    }

    /**
     * 归还当前租赁物品
     *
     * @param godsId
     * @param userId
     * @param startTime
     * @param endTime
     */
    @Override
    @Transactional
    public void repay(Integer godsId, Integer userId, String startTime, String endTime) {
        dhGodsrentInfoDao.repay(godsId, userId, startTime, endTime);
        DhGodsInfoEntity dhGodsInfoEntity = dhGodsInfoDao.selectById(godsId);
        Integer amount = dhGodsInfoEntity.getAmount();
        dhGodsInfoEntity.setAmount(amount + 1);
        LambdaQueryWrapper<DhGodsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhGodsInfoEntity::getId, godsId);
        dhGodsInfoDao.update(dhGodsInfoEntity, queryWrapper);
    }
}